import axios from "../../../../utils/axios";
Page({
  data: {
    courseList: [],
    goodsList: [],
    isShake: false
  },
  onLoad: async function (options) {
    this.getCourseOrder();
    this.getGoodsOrder();
  },
  async getCourseOrder() {
    let res = await axios({
      url: `/api/course/order/self`,
      header: {
        'Authorization': wx.getStorageSync('token')
      }
    });
    console.log(res.data.content);
    this.setData({
      courseList: res.data.content
    })
  },
  async getGoodsOrder() {
    let res = await axios({
      url: `/api/store/order/self?orderStatus=-9&orderType=1`,
      header: {
        'Authorization': wx.getStorageSync('token')
      }
    });
    console.log(res.data.content);

    this.setData({
      goodsList: res.data.content.filter(item => item.isDel == false || 0)
    })
  },
  async goToPay(e) {
    if (this.data.isShake == true)
      return;
    wx.showToast({
      title: '加工中...',
      icon: 'none',
      image: '',
      duration: 1500,
      mask: false,
    });
    return;
    let data = e.currentTarget.dataset.data;
    let type = e.currentTarget.dataset.ordertype;
    wx.navigateTo({
      url: './ordersHandle/ordersHandle',
      success: (res) => {
        res.eventChannel.emit('orderHandle', {
          state: 'unpaid',
          data,
          type
        })
      },
      fail: err => console.log(err)
    });
  },
  delOrder() {
    wx.vibrateShort({
      success: (result) => {

      },
      fail: () => {},
      complete: (res) => {
        console.log(res)
      }
    });
    this.setData({
      isShake: true
    })
  },
  async delOrderBtn(e) {
    let type = e.currentTarget.dataset.ordertype;

    if (e.currentTarget.dataset.data.payStatus == 1 || e.currentTarget.dataset.data.paid == 1) {
      this.setData({
        isShake: false
      }, wx.showToast({
        title: '已支付订单不可删除',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false,
      }))
      return;
    }
    if (type == 'course') {
      // 商品订单删除
      let id = e.currentTarget.dataset.data.id;

      let res = await axios({
        url: `/api/course/order/${id}`,
        method: 'delete',
        header: {
          'Authorization': wx.getStorageSync('token')
        }
      })
      wx.showToast({
        title: '课程订单已删除',
        icon: 'success',
        duration: 1500,
        mask: false,
      });
      this.setData({
        isShake: false,
        courseList: []
      })
      await this.getCourseOrder();
    } else if (type == 'goods') {
      // 商品订单删除
      let id = e.currentTarget.dataset.data.id;
      let res = await axios({
        url: `/api/store/order/self/${id}`,
        method: 'delete',
        header: {
          'Authorization': wx.getStorageSync('token')
        }
      })
      wx.showToast({
        title: '商品订单已删除',
        icon: 'success',
        duration: 1500,
        mask: false,
      });
      this.setData({
        isShake: false,
        goodsList: []
      })
      await this.getGoodsOrder();
    }

  },
  none() {
    this.setData({
      isShake: false
    })
  },
})